/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.tedu.mall.front.service.impl;

import cn.tedu.mall.common.exception.CoolSharkServiceException;
import cn.tedu.mall.common.pojo.product.vo.AttributeStandardVO;
import cn.tedu.mall.common.pojo.product.vo.SkuStandardVO;
import cn.tedu.mall.common.pojo.product.vo.SpuDetailStandardVO;
import cn.tedu.mall.common.pojo.product.vo.SpuListItemVO;
import cn.tedu.mall.common.pojo.product.vo.SpuStandardVO;
import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.ResponseCode;
import cn.tedu.mall.front.service.IFrontProductService;
import cn.tedu.mall.product.service.front.IForFrontAttributeService;
import cn.tedu.mall.product.service.front.IForFrontSkuService;
import cn.tedu.mall.product.service.front.IForFrontSpuService;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class FrontProductServiceImpl implements IFrontProductService {
    private static final String SPU_SIMPLE_VO_PREFIX = "SPU:SIMPLE:VO:PREFIX:";
    @DubboReference
    private IForFrontSpuService dubboSpuService;
    @DubboReference
    private IForFrontSkuService dubboSkuService;
    @DubboReference
    private IForFrontAttributeService dubboAttributeService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public JsonPage<SpuListItemVO> listSpuByCategoryId(Long categoryId, Integer page, Integer pageSize) {
        //调用方法获取返回值
        JsonPage<SpuListItemVO> spuListItemVOJsonPage = dubboSpuService.listSpuByCategoryId(categoryId, page, pageSize);
        //异常检验
        if (spuListItemVOJsonPage.getList().size() == 0) {
            throw new CoolSharkServiceException(ResponseCode.BAD_REQUEST, "您当前分类下没有任何商品");
        }
        return spuListItemVOJsonPage;
    }

    @Override
    public SpuStandardVO getFrontSpuById(Long id) throws CoolSharkServiceException {
        String spuSimpleVOKey = SPU_SIMPLE_VO_PREFIX + id;
        if (redisTemplate.hasKey(spuSimpleVOKey)) {
            return (SpuStandardVO) redisTemplate.boundValueOps(spuSimpleVOKey).get();
        }
        SpuStandardVO spuSimpleVO = null;
        spuSimpleVO = dubboSpuService.getSpuById(id);
        if (spuSimpleVO == null) {
            redisTemplate.boundValueOps(spuSimpleVOKey).set(null, 10, TimeUnit.SECONDS);
            throw new CoolSharkServiceException(ResponseCode.BAD_REQUEST, "该Id没有对应spu");
        }
        redisTemplate.boundValueOps(spuSimpleVOKey).set(spuSimpleVO, 10, TimeUnit.MINUTES);
        return spuSimpleVO;
    }

    @Override
    public List<SkuStandardVO> getFrontSkusBySpuId(Long spuId) {
        List<SkuStandardVO> frontSkus = dubboSkuService.getSkusBySpuId(spuId);
        if (frontSkus == null || frontSkus.size() == 0) {
            throw new CoolSharkServiceException(ResponseCode.BAD_REQUEST, "您访问的spu不存在sku数据");
        }
        return frontSkus;
    }

    @Override
    public SpuDetailStandardVO getSpuDetail(Long spuId) {
        //调用微服务查询detail
        SpuDetailStandardVO spuDetailStandardVO = dubboSpuService.getSpuDetailById(spuId);
        if (spuDetailStandardVO == null) {
            throw new CoolSharkServiceException(ResponseCode.BAD_REQUEST, "您访问的spuDetails不存在数据");
        }
        return spuDetailStandardVO;
    }

    @Override
    public List<AttributeStandardVO> getSpuAttributesBySpuId(Long spuId) {
        List<AttributeStandardVO> attributeStandardVOs = dubboAttributeService.getSpuAttributesBySpuId(spuId);
        if (attributeStandardVOs == null || attributeStandardVOs.size() == 0) {
            throw new CoolSharkServiceException(ResponseCode.BAD_REQUEST, "您访问的当前spu下没有sku的任何属性");
        }
        return attributeStandardVOs;
    }
}
